package mutsumimod.cards.copy;

import com.badlogic.gdx.Gdx;
import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.evacipated.cardcrawl.modthespire.lib.SpirePostfixPatch;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.green.WraithForm;
import com.megacrit.cardcrawl.cards.purple.DevaForm;
import com.megacrit.cardcrawl.cards.red.DemonForm;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.screens.SingleCardViewPopup;
import mutsumimod.cards.BaseCard;
import mutsumimod.character.Mutsumi;
import mutsumimod.powers.MultiHeadPower;
import mutsumimod.util.CardStats;

import java.util.ArrayList;

public class MultiHead extends BaseCard  {
    public static final String ID = makeID(MultiHead.class.getSimpleName());
    public static final CardStats info = new CardStats(
            Mutsumi.Meta.CARD_COLOR,
            CardType.POWER,
            CardRarity.RARE,
            CardTarget.SELF,
            3
    );
    private static final ArrayList<AbstractCard> previewList = new ArrayList<>();
    private float previewTimer = 1.0F;
    private int previewListIndex = 0;

    public MultiHead() {
        super(ID, info);
        setAttr(0,0,0);
        setAttrUpg(0,0,0);
        originShouldCopyUse = true;
        setShouldCopyUse(true);
        this.cardsToPreview = previewList.get(this.previewListIndex);
    }

    @Override
    public void beforeCopy(){
        clearCopyCardsAddSelf();
        addCopyCards(new DemonForm());
        addCopyCards(new WraithForm());
        addCopyCards(new DevaForm());
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        applyPowerSelf(new MultiHeadPower(p));
    }

    @Override
    public AbstractCard makeCopy() { //Optional
        return new MultiHead();
    }

    @Override
    public void updateDescription() {
        if(getInCopyUse()){
            rawDescription = cardStrings.EXTENDED_DESCRIPTION[0];
        }else{
            rawDescription = cardStrings.DESCRIPTION;
        }
        initializeDescription();
    }

    @Override
    public void upgrade() {
        if(!upgraded){
            upgradeName();
            upgradeBaseCost(baseCost-1);
        }
    }

    @Override
    public void update() {
        super.update();
        this.updatePreview();
    }

    public void updatePreview() {
        if (this.previewTimer > 0.0F) {
            this.previewTimer -= Gdx.graphics.getDeltaTime();
            return;
        }
        this.previewTimer = 1.0F;
        this.previewListIndex++;
        if (this.previewListIndex >= previewList.size()) {
            this.previewListIndex = 0;
        }
        this.cardsToPreview = previewList.get(this.previewListIndex);

    }

    @SpirePatch(clz = SingleCardViewPopup.class, method = "update", paramtypez = {void.class})
    public static class PreviewKeepUpdate {
        @SpirePostfixPatch
        public static void Postfix(SingleCardViewPopup __instance, AbstractCard ___card) {
            if (___card instanceof MultiHead) {
                ((MultiHead) ___card).updatePreview();
            }
        }
    }

    static {
        previewList.add(new DemonForm());
        previewList.add(new WraithForm());
        previewList.add(new DevaForm());
    }


}